package io.admin.modules.sys.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.admin.common.constant.Constant;
import io.admin.common.page.PageData;
import io.admin.common.service.impl.CrudServiceImpl;
import io.admin.common.utils.ConvertUtils;
import io.admin.common.utils.DateUtils;
import io.admin.modules.security.user.SecurityUser;
import io.admin.modules.security.user.UserDetail;
import io.admin.modules.sys.dao.UserDao;
import io.admin.modules.sys.dto.ProjectDTO;
import io.admin.modules.sys.dto.SysUserDTO;
import io.admin.modules.sys.dto.UserCountDTO;
import io.admin.modules.sys.dto.UserDTO;
import io.admin.modules.sys.entity.ProjectEntity;
import io.admin.modules.sys.entity.SysUserEntity;
import io.admin.modules.sys.entity.UserEntity;
import io.admin.modules.sys.entity.UserEntityPage;
import io.admin.modules.sys.enums.SuperAdminEnum;
import io.admin.modules.sys.service.UserService;
import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户表
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2024-07-04
 */
@Service
public class UserServiceImpl extends CrudServiceImpl<UserDao, UserEntity, UserDTO> implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public  PageData page(Map<String, Object> params){
        //转换成like
        paramsToLike(params, "projectName");
        paramsToLike(params, "username");
        paramsToLike(params, "mobile");
        //分页
        IPage<UserEntity> page = getPage(params,  "", false);
        //查询
        List<UserEntityPage> list = userDao.getListPage(params);
        if(!CollectionUtil.isEmpty(list)){
            for (UserEntityPage p:list){
                params =new HashMap<>();
                params.put("id",p.getId());
                List<ProjectEntity> listPro = userDao.getListProName(params);
                if(!CollectionUtil.isEmpty(listPro)){
                    p.setListIds(listPro);
                }
            }
        }
        return new PageData(list,page.getTotal());
    }


    @Override
    public  PageData<UserDTO> pageByPro(Map<String, Object> params){
        //转换成like
        paramsToLike(params, "username");
        paramsToLike(params, "realName");
        paramsToLike(params, "mobile");
        paramsToLike(params, "projectRole");
        //分页
        IPage<UserEntity> page = getPage(params,"", false);
        //查询
        List<UserEntity> list = userDao.getList(params);
        return getPageData(list, page.getTotal(), UserDTO.class);
    }

    @Override
    public  List<UserDTO> listByPro(Map<String, Object> params){

        List<UserEntity> list = userDao.getList(params);
       return ConvertUtils.sourceToTarget(list, UserDTO.class);
    }
    @Override
    public QueryWrapper<UserEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(id), "id", id);

        return wrapper;
    }

    @Override
    public UserCountDTO userCount(){
     String newData =   DateUtils.format(new Date(), "yyyy-MM-dd");
        UserCountDTO userCountDTO =new UserCountDTO();
        userCountDTO.setUserAll(userDao.userCount(""));
        userCountDTO.setNewToday(userDao.userCount(newData));
        LocalDate today = LocalDate.now(); // 获取今天的日期
        LocalDate yesterday = today.minusDays(1); // 减去一天，得到昨天的日期
        Date date2 = Date.from(yesterday.atStartOfDay(ZoneId.systemDefault()).toInstant());
        String yesterdays =   DateUtils.format(date2, "yyyy-MM-dd");
        userCountDTO.setAddedYesterday(userDao.userCount(yesterdays));
        return userCountDTO;
    }
}